package com.kgcx.coupon.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.utils.DateTimeUtils;
import com.kgcx.coupon.domain.Complain;
import com.kgcx.coupon.mapper.ComplainMapper;
import com.kgcx.coupon.rabbitmq.producer.OpcProducer;
import com.kgcx.dbutils.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/12/7 16:29
 * @since 0.1
 */
@Service
public class ComplainService {

    public static Logger logger = LoggerFactory.getLogger(ComplainService.class);

    @Autowired
    private ComplainMapper complainMapper;
    @Autowired
    private OpcProducer opcProducer;

    /**
     * 分页查询投诉信息
     * @param rd
     * @return
     */
    public List<Complain> queryComplainWithPage(RequestData rd){
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<Complain> list = complainMapper.selectComplainList(rd);
        for (Complain com :list){
            opcProducer.sendComplain(com);
        }
        return list;

    }

    /**
     * 新增投诉信息
     * @param orderNo
     * @param detail
     * @param result
     * @return
     */
    public Integer addComplain(String orderNo,String detail, String result){
        Complain complain = new Complain();
        complain.setOrderNo(orderNo);
        complain.setDetail(detail);
        complain.setResult(result);
        long dateTime =  DateTimeUtils.convertNumberSec(LocalDateTime.now());
        complain.setComplaintTime(dateTime);
        Integer one = complainMapper.insert(complain);
         try {
         //TODO 发送消息给监管平台
            opcProducer.sendComplain(complain);
         } catch (Exception e){
            logger.error("orderNo:"+orderNo+"comment send comment error",e);
        }
        return one;
    }

}
